export default {
    inject: {
        screenVm: {
            default: () => {
                return "";
            }
        }
    },
    props: {
        "config": {
            type: Object,
            default: () => {
            }
        },
        "screenConfig": {
            type: Object,
            default: () => {
            }
        },
        //路由懒加载文件目录
        loadView:{
            type: Function
        },
    },
    data() {
        return {
            viewVm: ""
        }
    },
    created() {
        let self_ = this;
        this.$bus.on('customViewEvent', (args) => {
            if (args.type == "props") {
                self_.handProps(args, self_)
            } else if (args.type == "event") {
                self_.handEvent(args, self_)
            }
        });
    },
    mounted() {
        this.viewVm = this.$refs.controllerView;
        this.initWatch();
        this.init();
    },
    methods: {
        getFile(customName) {
            let file_ = "";
            let files = import.meta.globEager('@/views/modules/custom-components/**/*.json');
            for (let key in files) {
                let file = files[key]["default"];
                if (file && customName == file.name) {
                    file_ = file;
                    break;
                }
            }
            return file_;
        },
        initWatch() {
            let propsList = [];
            let file = this.getFile(this.config.customName);
            propsList = file.props;
            let self = this;
            for (let i = 0; i < propsList.length; i++) {
                this.viewVm.$watch(propsList[i].name, function (val) {
                    let obj = {};
                    obj[propsList[i].name] = val
                    self.$bus.emit("customViewEvent", {type: "props", component: self.config.key, params: obj});
                }, {
                    deep: true
                })
            }
        },
        init() {
            //属性
            if (this.config.props) {
                for (let key in this.config.props) {
                    let propsConfig = this.config.props[key];
                    //固定值
                    if (propsConfig.valueSource == "fixedVar") {
                        this.viewVm[key] = propsConfig.propsValue;
                    } else if (propsConfig.valueSource == "script" && propsConfig.propsValue) {//脚本方法
                        if (this.screenVm[propsConfig.propsValue]) {
                            this.$nextTick(function () {
                                this.viewVm[key] = this.screenVm[propsConfig.propsValue](this.viewVm);
                            })
                        }
                    }
                }
            }
        },
        //处理属性事件
        handProps(args, self_) {
            if (this.config.props) {
                for (let key in this.config.props) {
                    let propsConfig = this.config.props[key];
                    if (propsConfig.component != args.component) {
                        continue;
                    }
                    //组件
                    if (propsConfig.valueSource == "component" && args.params) {
                        this.viewVm[key] = args.params[propsConfig.propsValue];
                    }
                }
            }
        },
        //处理事件
        handEvent(args, self_) {
            if (args.component != this.config.key) {
                return;
            }
            let func = "this.viewVm." + args.method + "("
            if (args.params && args.params.length > 0) {
                for (let i = 0; i < args.params.length; i++) {
                    if (i > 0) {
                        func += ","
                    }
                    let param = args.params[i];
                    func += `'${param.value || ""}'`;
                }
            }
            func += ")";
            eval(func);
        },
        //处理组件的事件
        handViewEvent(args, eventKey) {
            if (this.config.events) {
                for (let key in this.config.events) {
                    if (eventKey != key) {
                        continue;
                    }
                    let eventConfig = this.config.events[key] ? JSON.parse(JSON.stringify(this.config.events[key])) : [];
                    for (let i = 0; i < eventConfig.length; i++) {
                        if (eventConfig[i].params) {
                            for (let j = 0; j < eventConfig[i].params.length; j++) {
                                let params = eventConfig[i].params[j];
                                if (params.valueSource == "param") {//获取事件传递的参数
                                    params.value = this.getEventParams(args, eventKey, params.eventValue);
                                } else if (params.valueSource == "fixedVar") {//固定值
                                    params.value = params.eventValue;
                                } else if (params.valueSource == "component") {//指定组件的props
                                    let pointVm = this.screenVm.$refs[params.component][0].$refs.customComponent.$refs.controllerView;
                                    params.value = pointVm[params.eventValue];
                                } else if (params.valueSource == "script") {//脚本方法
                                    if (this.screenVm[params.eventValue]) {
                                        params.value = this.screenVm[params.eventValue](eventKey, args, this.viewVm);
                                    }
                                }
                            }
                        }
                        this.$bus.emit("customViewEvent", Object.assign({type: "event"}, eventConfig[i]));
                    }
                }
            }
        },
        /**
         * 获取事件参数值
         * @param args 事件传递的参数
         * @param eventKey 事件key
         * @param eventValue 获取的参数
         * @returns {string}
         */
        getEventParams(args, eventKey, eventValue) {
            let value = "";
            let file = this.getFile(this.config.customName);
            if (file.events) {
                for (let i = 0; i < file.events.length; i++) {
                    let event = file.events[i];
                    if (event.name == eventKey) {
                        if (event.params) {
                            for (let j = 0; j < event.params.length; j++) {
                                if (event.params[j].name == eventValue) {
                                    value = args[j];
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return value;
        }
    }
}
